package com.tencent.wework.list

 import androidx.recyclerview.widget.GridLayoutManager
 import androidx.recyclerview.widget.RecyclerView
 import androidx.recyclerview.widget.StaggeredGridLayoutManager
 import com.tencent.wework.list.cell.*


/**
 * Created by huangwei on 2019/5/26.
 */
class LmAdapter():BaseRecyclerAdapter<BaseRecyclerCell<out Any>>(){

    companion object{
        const val TYPE_EMPTY = Integer.MAX_VALUE - 1
        const val TYPE_ERROR = Integer.MAX_VALUE - 2
        const val TYPE_LODING = Integer.MAX_VALUE - 3
        const val TYPE_NO_MORE = Integer.MAX_VALUE - 4
        const val TYPE_LOADING_MORE = Integer.MAX_VALUE - 5
    }


    var emptyCell:BaseRecyclerCell<out Any>
    var errorCell:BaseRecyclerCell<out Any>
    var refreshCell:BaseRecyclerCell<out Any>
    var noMoreCell:BaseRecyclerCell<out Any>
    var loadingMoreCell:BaseRecyclerCell<out Any>

    init {
        loadingMoreCell = LoadingMoreCell()
        emptyCell = EmptyCell()
        errorCell = ErrorCell()
        refreshCell = RefreshingCell()
        noMoreCell = NoMoreCell()
    }

    fun showEmpty() {
        if (isEmpty()) {
            return
        }
        clear()
        add(emptyCell)
    }

    fun showError() {
        if (isError()) {
            return
        }
        clear()
        add(errorCell)
    }

    fun showLoading() {
        if (isRefreshing()) {
            return
        }
        clear()
        add(refreshCell)
    }

    fun showNoMoreContent(loadMoreDirection: LoadMoreDirection=LoadMoreDirection.BOTTOM) {
        if (!cells.contains(noMoreCell)) {
            when(loadMoreDirection){
                LoadMoreDirection.BOTTOM ->{
                    add(noMoreCell)
                }
                LoadMoreDirection.TOP ->{
                    add(0,noMoreCell)
                }
            }
        }
    }

    fun hideEmpty() {
        remove(emptyCell)
    }

    fun hideLoading() {
        remove(refreshCell)
    }

    fun hideError() {
        remove(errorCell)
    }

    fun hideNoMoreContent() {
        remove(noMoreCell)
    }

    fun showLoadingMore(loadMoreDirection: LoadMoreDirection = LoadMoreDirection.BOTTOM) {
        when(loadMoreDirection){
            LoadMoreDirection.BOTTOM ->{
                add(loadingMoreCell)
            }
            LoadMoreDirection.TOP ->{
                add(0,loadingMoreCell)
            }
        }
    }

    fun hideLoadingMore() {
        remove(loadingMoreCell)
    }

    /**
     * 添加时会执行{@link #hideLoadingMore()}
     */
    fun appendCells(cellList: List<BaseRecyclerCell<out Any>>,loadMoreDirection: LoadMoreDirection = LoadMoreDirection.BOTTOM) {
        beginChange()
        hideLoadingMore()
        if (cellList.isEmpty()) {
            endChange(true)
            return
        }
        when(loadMoreDirection){
            LoadMoreDirection.BOTTOM -> addAll(cellList)
            LoadMoreDirection.TOP -> addAll(0,cellList)
        }
        endChange(true)
    }

    override fun onAttachedToRecyclerView(recyclerView: RecyclerView) {
        super.onAttachedToRecyclerView(recyclerView)
        val manager = recyclerView.layoutManager
        if (manager is GridLayoutManager) {
            val gridManager = manager as GridLayoutManager?
            gridManager?.spanSizeLookup = object : GridLayoutManager.SpanSizeLookup() {
                override fun getSpanSize(position: Int): Int {
                    val type = getItemViewType(position)
                    return if (getItem(position) is FullSpan
                        || type == TYPE_LOADING_MORE
                        || type == TYPE_EMPTY
                        || type == TYPE_ERROR
                        || type == TYPE_LODING
                        || type == TYPE_NO_MORE
                    )
                        gridManager!!.spanCount
                    else
                        1
                }
            }
        }
    }

    override fun onViewAttachedToWindow(holder:BaseViewHolder) {
        super.onViewAttachedToWindow(holder)
        if (holder.itemView.layoutParams is StaggeredGridLayoutManager.LayoutParams) {
            val type = holder.itemViewType
            if (getItem(holder.adapterPosition) is FullSpan
                || type == TYPE_LOADING_MORE
                || type == TYPE_EMPTY
                || type == TYPE_LODING
                || type == TYPE_NO_MORE
                || type == TYPE_ERROR
            ) {
                val p = holder.itemView.layoutParams as StaggeredGridLayoutManager.LayoutParams
                p.isFullSpan = true
            }
        }
    }

    fun isLoadingMore(): Boolean {
        return indexOf(loadingMoreCell) != -1
    }

    fun isEmpty(): Boolean {
        return cells.contains(emptyCell)
    }

    fun isError(): Boolean {
        return cells.contains(errorCell)
    }

    fun isRefreshing(): Boolean {
        return cells.contains(refreshCell)
    }

    fun isNoMoreShowing(): Boolean {
        return cells.contains(noMoreCell)
    }

    interface FullSpan
}